package com.pureut.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.domain.NonconformingProductReview;
import com.pureut.system.domain.vo.NonconformingProductReviewVo;
import com.pureut.system.mapper.NonconformingProductReviewMapper;
import com.pureut.system.service.INonconformingProductReviewService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * @Author: pureut
 * @Date: 2024-12-06 13:51:19
 * @Desc: 不合格品审
 */
@Service("nonconformingProductReviewService")
public class NonconformingProductReviewServiceImpl extends ServiceImpl<NonconformingProductReviewMapper, NonconformingProductReview> implements INonconformingProductReviewService {

    /**
     * 不合格品审Dao
     */
    @Resource
    private NonconformingProductReviewMapper nonconformingProductReviewDao;

    /**
     * 查询列表
     *
     * @param nonconformingProductReviewVo
     * @return
     */
    @Override
    public List<NonconformingProductReview> queryPage(NonconformingProductReviewVo nonconformingProductReviewVo) {
        // 获取认证方式的数据字典
        List<SysDictData> netArray = DictUtils.getDictCache("unqualified_level");
        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        List<NonconformingProductReview> list=nonconformingProductReviewDao.selectByPage( nonconformingProductReviewVo);
        list.forEach(e->{
            e.setFailureLevel(netMap.get(String.valueOf(e.getFailureLevel())));

        });

        // 总数、结果
        return list;
    }


    /**
     * 新增不合格品审
     *
     * @param nonconformingProductReviewVo
     * @return
     */
    @Override
    public Boolean save(NonconformingProductReviewVo nonconformingProductReviewVo) {
        NonconformingProductReview nonconformingProductReview = new NonconformingProductReview();
        BeanUtil.copyProperties(nonconformingProductReviewVo, nonconformingProductReview);
        nonconformingProductReview.setCreateBy(SecurityUtils.getUsername());
        nonconformingProductReview.setCreateTime(new Date());
        return save(nonconformingProductReview);
    }


    /**
     * 更新不合格品审
     *
     * @param nonconformingProductReviewVo
     * @return
     */
    @Override
    public Boolean updateById(NonconformingProductReviewVo nonconformingProductReviewVo) {
        NonconformingProductReview nonconformingProductReview = new NonconformingProductReview();
        BeanUtil.copyProperties(nonconformingProductReviewVo, nonconformingProductReview);
        nonconformingProductReview.setUpdateBy(SecurityUtils.getUsername());
        nonconformingProductReview.setUpdateTime(new Date());
        return updateById(nonconformingProductReview);
    }
}
